package com.fitbet.admin.service.bet.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fitbet.admin.dao.UserBetDao;
import com.fitbet.admin.handler.SmsHandler;
import com.fitbet.admin.service.bet.UserBetService;
import com.fitbet.admin.service.impl.DataServiceImpl;
import com.fitbet.admin.web.Constant;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.bet.UserBetSupport;
import com.fitbet.entity.user.Wallet;
/**
 * 
 * @author sjh
 *
 */
@Service("userBetService")
public class UserBetServiceImpl extends DataServiceImpl<UserBet> implements UserBetService{

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	

	@Dao
	private UserBetDao userBetDao;
	
	@Autowired
	private SmsHandler SmsHandler;
	
	@Value("#{config['wechat.host']}")
	private String wechatHost;
	
	
	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.UserBetService#count()
	 */
	public int count(UserBetStatus status) {
		return userBetDao.count(status);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.UserBetService#list()
	 */
	public List<UserBet> list(UserBetStatus status,Page page) {
		
		return userBetDao.list(status, page.getBeginIndex(), page.getEveryPage());
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.UserBetService#auth(java.lang.Long, boolean)
	 */
	public void auth(Long id, boolean passed) {
		
		UserBet userBet = genericDaoSupport.load(UserBet.class, id);
		
		genericDaoSupport.update(userBet.auth(passed));
		
		if(passed) {
			
			 String content ="【"+Constant.SMS_COMPANY_NAME+"】"+ "恭喜您！在FITBET平台上创建的 " + userBet.getBet().getName() + " 挑战活动已通过审核，点击链接  "+ wechatHost+"/profile " + "，快邀请您的伙伴们来加油助威吧！";
			
			 SmsHandler.sendMessage(userBet.getUser().getMobile(), content);
			
		} else {
			
			String content ="【" + Constant.SMS_COMPANY_NAME + "】" + "很遗憾，您在FITBET平台上创建的" + userBet.getBet().getName() + "挑战活动未能通过审核，点击链接  " + wechatHost+"/profile "+ "重新提交，来参与精彩活动。";
		
			SmsHandler.sendMessage(userBet.getUser().getMobile(), content);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.UserBetService#close(java.lang.Long, boolean)
	 */
	public void close(Long id, boolean reload, boolean success) {
		
		UserBet userBet = genericDaoSupport.load(UserBet.class, id);
		if(reload) {
			genericDaoSupport.update(userBet.afresh());
			String content ="【"+Constant.SMS_COMPANY_NAME+"】"+"很遗憾，您在FITBET平台上提交的视频未能通过审核，点击链接 "+wechatHost+"/profile "+"重新提交，预祝您挑战成功。";
			SmsHandler.sendMessage(userBet.getUser().getMobile(), content);
			return;
		}
		
		genericDaoSupport.update(userBet.closes(success));
		
		// 计算价格，根据状态更新挑战者信息，给挑战者发短信
		int bonusPool = userBet.getBonus();
		BigDecimal supportResults = new BigDecimal(bonusPool).multiply(new BigDecimal(94)).divide(new BigDecimal(100));
		BigDecimal bouns = new BigDecimal(0);
		if(success) {
			bouns = supportResults.divide(new BigDecimal(userBet.getThumbup()));
			genericDaoSupport.update(userBet.income(bouns.intValue()));
			String content ="【"+Constant.SMS_COMPANY_NAME+"】"+"恭喜您！在FITBET平台上参与的"+userBet.getBet().getName()+"活动挑战成功，最终奖励结果将于1-2天内揭晓，敬请期待。"; 
			SmsHandler.sendMessage(userBet.getUser().getMobile(), content);
		} else {
			if(userBet.getThumbdown()>0) {
				bouns = supportResults.divide(new BigDecimal(userBet.getThumbdown()));
				
			}
			String content ="【"+Constant.SMS_COMPANY_NAME+"】"+"您参与的"+userBet.getBet().getName()+"挑战活动最终结果现已揭晓，很遗憾您未能挑战成功，请登录 " +wechatHost+"/profile "+"查询，欢迎持续关注。";
			SmsHandler.sendMessage(userBet.getUser().getMobile(), content);
		}
		BigDecimal price = bouns.divide(new BigDecimal(100));
		// 更新竟猜者已完成，获取奖金，发送短信
		List<UserBetSupport> userBetSupports = userBetDao.listById(userBet.getId());
		for(UserBetSupport userBetSupport:userBetSupports) {
			
			String faildContent ="【"+Constant.SMS_COMPANY_NAME+"】"+"您关注的"+userBetSupport.getUserBet().getUser().getNickname()+userBetSupport.getUserBet().getBet().getName()+"挑战活动最终结果现已揭晓，很遗憾您未能赢得本次活动的奖金，详情请登录 "+wechatHost+"/profile "+"查询，欢迎持续关注。";
			String successContent ="【"+Constant.SMS_COMPANY_NAME+"】"+"您关注的"+userBetSupport.getUserBet().getUser().getNickname()+userBetSupport.getUserBet().getBet().getName()+"挑战活动最终结果现已揭晓，恭喜您获得奖金"+price+"元人民币，详情请登录 "+wechatHost+"/profile "+"查询。";
			if(success == userBetSupport.isSupport()) {  
				// success == true && support == true / success == false && support == false
				genericDaoSupport.update(userBetSupport.bounsResult(bouns.intValue()).finish());
				Wallet userWallet = userBetDao.load(userBetSupport.getUser().getId());
				genericDaoSupport.update(userWallet.income(bouns.intValue()));
				if(userBet.getUser().getId() != userBetSupport.getUser().getId()) {
					SmsHandler.sendMessage(userBetSupport.getUser().getMobile(), successContent);
				}
				
			} else {
				genericDaoSupport.update(userBetSupport.finish());
				if(userBet.getUser().getId() != userBetSupport.getUser().getId()) {
					SmsHandler.sendMessage(userBetSupport.getUser().getMobile(), faildContent);
				}
			}
			
		}
		
	}


	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.UserBetService#failure()
	 */
	public void failure(Long id) {
		UserBet userBet = genericDaoSupport.load(UserBet.class, id);
		genericDaoSupport.update(userBet.failure());
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.UserBetService#listById(java.lang.Long)
	 */
	public List<UserBetSupport> listById(Long id) {
		
		return userBetDao.listById(id);
		
	}


}
